package com.shuqi.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

import com.shuqi.interfaces.EventTypeConstants;

public class ExampleModel extends ModelBase {

    private Context mContext;
    private String mData;

    public ExampleModel(Context context) {
        mContext = context;
    }

    public void changeData(String text) {
        try {
            FileOutputStream fos = mContext.openFileOutput("1.txt", 0);
            fos.write(text.getBytes());
            fos.close();
        } catch (FileNotFoundException e) {
            fireEvent(EventTypeConstants.CONTENT_WRITE_ERROR, "write error: FileNotFoundException");
        } catch (IOException e) {
            fireEvent(EventTypeConstants.CONTENT_WRITE_ERROR, "write error: IOException");
        }

        loadData();
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub

    }

    @Override
    public void loadData(Object... objs) {
        try {
            FileInputStream fis = mContext.openFileInput("1.txt");
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            mData = new String(buffer);
            fis.close();
        } catch (FileNotFoundException e) {
            fireEvent(EventTypeConstants.CONTENT_READ_ERROR, "read error: FileNotFoundException");
        } catch (IOException e) {
            fireEvent(EventTypeConstants.CONTENT_READ_ERROR, "read error: IOException");
        }

        fireEvent(EventTypeConstants.CONTENT_LOADED, mData);
    }
}
